import { WeaForm, WeaSwitch } from 'comsMobx';
import { observable, action, toJS } from 'mobx';
import { inject, observer } from 'mobx-react';
import React from 'react';
import { WeaSearchGroup, WeaFormItem, WeaTools, WeaErrorPage,WeaUpload,WeaLocaleProvider  } from 'ecCom';
import { Button,Row,Col } from 'antd';
const getLabel = WeaLocaleProvider.getLabel;

@observer
class CptInventoryPlanForm extends React.Component {

    constructor(props) {
        super(props);
    }

    getFormCondition() {
        const { form, formcondition } = this.props;
        const { isFormInit } = form;
        let group = [];
        isFormInit && formcondition.map((c,i) => {
            let items = [];
            c.items.map(fields => {
                let hasunderline = false;
                if(fields.viewAttr == "1" || (fields.conditionType == "BROWSER" && fields.browserConditionParam.viewAttr == "1")) {hasunderline = true}  //只读样式控制

                if(fields.conditionType == "CPTIMG"){
                    let domkey = fields.domkey[0];
                    items.push({
                        com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@kg9cfx@${fields.domkey[0]}`}
                            error={form.getError(fields)}
                            tipPosition="bottom"
                            label={`${fields.label}`}
                            labelCol={{ span: `${fields.labelcol}` }}
                            wrapperCol={{ span: `${fields.fieldcol}` }} underline={hasunderline?true:false} >
                            <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@ubalmy@${fields.domkey[0]}`}>
                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@d07gt9@${fields.domkey[0]}`} span={24} >
                                <div className="cpt-img-upload">
                                    <WeaUpload ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@nchklg@${fields.domkey[0]}`}
                                        uploadId={[domkey]}
                                        uploadUrl={"/api/doc/upload/uploadFile"}
                                        category='all'
                                        autoUpload={true}
                                        showClearAll={false}
                                        multiSelection={true}
                                        datas={toJS(fields.datas)}
                                        listType='img'
                                        limitType='jpg,gif,png'
                                        imgWidth={50}
                                        imgHeight={50}
                                        maxUploadSize={5}
                                        onChange={v => {form.updateFields({[domkey]:{value:v}}, false) }}
                                        viewAttr={fields.viewAttr}
                                    />
                                    </div>
                                </Col>
                            </Row>
                        </WeaFormItem>),
                        colSpan: 1
                    })
                }else{
                    items.push({
                        com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@6a0j8r@${fields.domkey[0]}`}
                            error={form.getError(fields)}
                            tipPosition="bottom"
                            label={`${fields.label}`}
                            labelCol={{ span: `${fields.labelcol}` }}
                            wrapperCol={{ span: `${fields.fieldcol}` }} underline={hasunderline?true:false}>
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@2636u0@${fields.domkey[0]}`} fieldConfig={fields} form={form} />
                        </WeaFormItem>),
                        colSpan: 1
                    })
                }        
            });
            group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@vzhw60@${i}`} needTigger={false} col={1} title={c.title} showGroup={c.defaultshow} items={items} />)
        });
        return group;
    }

    render() {
        return (
            <div>
                {this.getFormCondition()}
            </div>
        )
    }
}

export default WeaTools.tryCatch(React,
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@oc8a9z`} msg={props.error ? props.error : getLabel( 383324,'对不起，该页面异常，请联系管理员' )} />,
    { error: "" }
)(CptInventoryPlanForm);